package top.kaoshanji.p1util;

import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * @author kaoshanji
 * ^_^
 * create time 2025/5/24 21:56
 */
public class ConfigUtil {

    /**
     * 获取客户端：第三方应用配置参数
     * @return ClientAppConfig
     */
    public static ClientAppConfig getClientAppConfig() {

        String configPath = "p1-ch03a-client-app.txt";
        ClientAppConfig config = new ClientAppConfig();

        Map<String, String> mapConfig = getTxtNameAndValue(configPath);
        if (null != mapConfig) {
            if (mapConfig.containsKey("client_id")) {
                config.setClientId(mapConfig.get("client_id"));
            }

            if (mapConfig.containsKey("client_secret")) {
                config.setClientSecret(mapConfig.get("client_secret"));
            }

            if (mapConfig.containsKey("redirect_uri")) {
                config.setRedirectUri(mapConfig.get("redirect_uri"));
            }

            if (mapConfig.containsKey("authorization_uri")) {
                config.setAuthorizationUri(mapConfig.get("authorization_uri"));
            }

            if (mapConfig.containsKey("token_uri")) {
                config.setTokenUri(mapConfig.get("token_uri"));
            }

            if (mapConfig.containsKey("resource_uri")) {
                config.setResourceUri(mapConfig.get("resource_uri"));
            }

        }

        return config;
    }

    /**
     * 获取授权服务器配置参数
     * @return Oauth2ServerConfig
     */
    public static Oauth2ServerConfig getOauth2ServerConfig() {
        String configPath = "p1-ch03b-oauth2-server.txt";
        Oauth2ServerConfig config = new Oauth2ServerConfig();

        Map<String, String> mapConfig = getTxtNameAndValue(configPath);
        if (null != mapConfig) {
            if (mapConfig.containsKey("client_id")) {
                config.setClientId(mapConfig.get("client_id"));
            }

            if (mapConfig.containsKey("client_secret")) {
                config.setClientSecret(mapConfig.get("client_secret"));
            }

            if (mapConfig.containsKey("redirect_uri")) {
                config.setRedirectUri(mapConfig.get("redirect_uri"));
            }

        }

        return config;
    }

    /**
     * 获取token参数
     * @return Oauth2TokenConfig
     */
    public static Oauth2TokenConfig getOauth2TokenConfig() {
        String configPath = "p1-ch03b-oauth2-token.txt";
        Oauth2TokenConfig config = new Oauth2TokenConfig();

        Map<String, String> mapConfig = getTxtNameAndValue(configPath);
        if (null != mapConfig) {
            if (mapConfig.containsKey("token")) {
                config.setToken(mapConfig.get("token"));
            }
        }

        return config;

    }


    private static Map<String, String> getTxtNameAndValue(String pathStr) {
        Map<String, String> map = null;

        try {
            map = new HashMap<>();

            Path path = Paths.get(FileConstant.LOCAL_ROOT_PATH.concat(pathStr));
            List<String> stringList = Files.readAllLines(path, StandardCharsets.UTF_8);

            for (String str: stringList) {
                // System.out.println(str);

                if (StringUtils.isNotEmpty(str)) {
                    String[] vl = str.split("=");

                    map.put(vl[0], vl[1]);

                }
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        return map;
    }

    public static void main(String[] args) {

        ClientAppConfig clientAppConfig = ConfigUtil.getClientAppConfig();
        System.out.println(clientAppConfig);
        System.out.println(clientAppConfig.getAuthorizationUri());

        Oauth2ServerConfig serverConfig = ConfigUtil.getOauth2ServerConfig();
        System.out.println(serverConfig);
        System.out.println(serverConfig.getRedirectUri());

        Oauth2TokenConfig tokenConfig = ConfigUtil.getOauth2TokenConfig();
        System.out.println(tokenConfig);
        System.out.println(tokenConfig.getToken());

        System.out.println(new Random().nextInt(6));

    }

}
